/**
 * @Title: UserController.java
 * @Package org.wk.user.controller
 * @Description: TODO【】
 * @author df.w
 * @date 2018年5月3日 下午2:40:54
 * @version V1.0
 */

package org.wk.user.controller;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.wk.account.service.SubAccountService;
import org.wk.core.common.tool.ExcelUtils;
import org.wk.core.common.view.PageVo;
import org.wk.core.common.view.ResponseVo;
import org.wk.logger.common.annotation.SysLog;
import org.wk.user.domain.User;
import org.wk.user.enums.IdentityType;
import org.wk.user.service.UserService;

import org.wk.user.view.UserVo;

import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName	: UserController
 * @Package		：org.wk.user.controller
 * @Author df.w
 * @Create Time	：2018年5月3日 下午2:40:54
 * @Description	: 
 */
@RestController
@RequestMapping("/users/user")
public class UserAdminController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private UserService userService;
	@Autowired
	private SubAccountService accountService;

	/**
	 * 用户列表
	 */
	@RequestMapping("/list")
	public ResponseVo list(@RequestParam Map<String, Object> params){
		PageVo page = userService.queryPage(params);
		return ResponseVo.success().addAttribute("page", page);
	}

	/*
	 * @description 用户详情
	 * @param
	 * @author ZhaoPing'an
	 * @date 2018/8/13 10:34
	 */
	@RequestMapping("/info/{userId}")
	public ResponseVo info(@PathVariable("userId") String id){

		ResponseVo responseVo = userService.queryUserDetailById(id);

		return responseVo;
	}

	/*
	 * @description 修改用户信息(包括账户信息)
	 * @param
	 * @author ZhaoPing'an
	 * @date 2018/8/13 10:35
	 */
	@SysLog("修改用户信息")
	@RequestMapping("/update")
	public ResponseVo update(@RequestBody UserVo user){
		ResponseVo responseVo = userService.updateUserDetailById(user);
		return responseVo;
	}

	/*
	 * @description 冻结用户
	 * @param
	 * @author ZhaoPing'an
	 * @date 2018/7/26 14:14
	 */
	@SysLog("冻结用户")
	@RequestMapping("/freezeUser")
	public ResponseVo freezeUser(@RequestBody User user){
		ResponseVo responseVo = userService.freezeUser(user.getId());
		return responseVo;
	}

	/*
	 * @description 冻结用户
	 * @param
	 * @author ZhaoPing'an
	 * @date 2018/7/26 14:14
	 */
	@SysLog("用户解冻")
	@RequestMapping("/unFreezeUser")
	public ResponseVo unFreezeUser(@RequestBody User user){
		ResponseVo responseVo = userService.unFreezeUser(user.getId());
		return responseVo;
	}

	/*
	 * @description 导出用户
	 * @param
	 * @author ZhaoPing'an
	 * @date 2018/7/26 15:02
	 */
	@SysLog("导出用户")
	@RequestMapping(value="/exportUsers")
	public void exportUsers(@RequestParam Map<String, Object> params,HttpServletResponse response){

		try {
			String[] rowsName = new String[]{"序号","姓名","昵称","性别","手机号","用户身份","身份证号","推荐人","注册时间"};
			// 获取填充数据
			List<Object[]> datas = userService.exportUsers(params,rowsName.length);
			String fileName = "users"+ new LocalDate().toString("yyyyMMdd") +".xls";
			// 生成Excel
			HSSFWorkbook workbook = ExcelUtils.getHSSFWorkbook("鲸拍用户表",rowsName,datas);
			OutputStream os = response.getOutputStream();
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition","attachment;filename="+fileName);
			workbook.write(os);
			os.flush();
			os.close();
		} catch (Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * 用户管理下的用户列表 每个用户详情的 子账号的变动记录
	 * @author TangShenShen
	 * @date 2018/5/29 11:48
	 */
	@RequestMapping(value="/accounts/{account_type}/seq",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public ResponseVo getSubAccountSeqs(@PathVariable("account_type") String accountType,
										@RequestParam(required = false,defaultValue = "1") Integer page_num, @RequestParam(required = false,defaultValue = "10") Integer page_size,@RequestParam(required = true) String userId){
		logger.info("当前用户的账户变动列表>>>>{}",accountType);
		ResponseVo responseVo = accountService.getSubAccountSeqs(userId,accountType,page_num,page_size);
		logger.info("当前用户的账户变动列表返回值>>>>{}",responseVo.toString());
		return responseVo;
	}

	@RequestMapping("/getIdentitys")
	public ResponseVo getIdentitys(){
		List<Map<String,String >> result = new ArrayList<>();
		for (IdentityType temp:IdentityType.values()){
			Map<String,String > identity = new HashMap<>();
			identity.put("code",temp.getCode()+"");
			identity.put("name",temp.getName());
			result.add(identity);
		}
		return ResponseVo.success().setData(result);
	}


}
